package com.upeoe.shiro.chapter2;

import org.apache.shiro.authz.UnauthorizedException;
import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;

/**
 * @author ven_hxz
 * @create 2016-07-03 上午11:21
 */
public class RoleTutorial extends LoginHelper {
    @Test
    public void testHasRole() {
        login("classpath:shiro-role.ini", "root", "root");

        Assert.assertTrue(getSubject().hasRole("role1"));
        Assert.assertTrue(getSubject().hasAllRoles(Arrays.asList("role1", "role2")));

        boolean[] result = getSubject().hasRoles(Arrays.asList("role1", "role2", "role3"));
        Assert.assertEquals(true, result[0]);
        Assert.assertEquals(true, result[1]);
        Assert.assertEquals(false, result[2]);
    }

    @Test(expected = UnauthorizedException.class)
    public void testCheckRole() {
        login("classpath:shiro-role.ini", "guest", "guest");
        getSubject().checkRole("role2");
        getSubject().checkRoles("role1", "role2");
    }
}
